<template>
  <div class="app-container">
    <el-form v-show="state.showSearch" :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="标签名称" prop="tagName">
        <el-input v-model="queryParams.tagName" placeholder="标签名称" />
      </el-form-item>
      <el-form-item label="状态" prop="enable">
        <el-select v-model="queryParams.enable" placeholder="请选择" clearable>
          <el-option
            v-for="dict in sys_common_staus"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="城市标签" prop="cityName">
        <el-select
          v-model="queryParams.cityName"
          @change="handleSearch(true)"
          fit-input-width
          placeholder="请选择"
        >
          <el-option
            v-for="item in cityNameOptions"
            :key="item"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="&nbsp;">
        <el-button @click="handleSearch(false)">重置</el-button>
        <el-button type="primary" @click="handleSearch(true)">查询</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['machine:tag:add']" @click="handleAdd">
          新增
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="state.showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="state.loading" :data="tableList">
      <el-table-column label="序号" align="center" type="index" :index="indexAdd" width="100" />
      <el-table-column
        label="标签名称"
        align="center"
        prop="tagName"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="城市标签" align="center" prop="cityName" show-overflow-tooltip />
      <el-table-column label="状态" align="center" prop="enable">
        <template #default="scope">
          <dict-tag :options="sys_common_staus" :value="scope.row.enable" />
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="updateOper" />
      <el-table-column label="操作时间" align="center" prop="updateTime" />
      <el-table-column label="操作" align="center" fixed="right" width="150">
        <template #default="{ row }">
          <el-button link type="primary" @click="handleTableAction(row, 'detail')">
            查看
          </el-button>
          <el-button
            link
            type="primary"
            v-hasPermi="['machine:tag:edit']"
            @click="handleTableAction(row, 'edit')"
          >
            编辑
          </el-button>

          <ConfirmButton
            v-hasPermi="['machine:tag:dis']"
            :row="row"
            tipText="此设备标签"
            @confirm="handleTableAction"
          />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="state.total > 0"
      :total="state.total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <ActionDialog ref="dialogRef" @refresh="getList" />
  </div>
</template>

<script setup name="DeviceTagManage">
import { getDeviceTagList, disableTag } from '@/api/device/tag'
import { onMountedOrActivated } from '@/hooks/common'
import ActionDialog from './components/ActionDialog'
import useGlobalStore from '@/store/modules/global'

const globalStore = useGlobalStore()
const cityNameOptions = computed(() => {
  queryParams.value.cityName = globalStore.myCityList.length ? globalStore.myCityList[0] : ''
  return globalStore.myCityList
})

const queryRef = ref(null)
const dialogRef = ref(null)
const { proxy } = getCurrentInstance()
const { sys_common_staus } = proxy.useDict('sys_common_staus')

const state = reactive({
  visible: false,
  showSearch: true,
  total: 0,
  loading: false
})
const queryParams = ref({
  pageNum: 1,
  pageSize: 10
})
const tableList = ref([])

function refresh() {
  queryParams.value.pageNum = 1
  getList()
}

function handleSearch(confirm) {
  !confirm && queryRef.value.resetFields()
  refresh()
}

// type序号 - 页面切换递增
function indexAdd(index) {
  const { pageNum, pageSize } = queryParams.value
  return index + 1 + (pageNum - 1) * pageSize
}

async function getList() {
  state.loading = true

  try {
    const r = await getDeviceTagList(queryParams.value)
    tableList.value = r.rows
    state.total = r.total
  } finally {
    state.loading = false
  }
}

/**table Action */
function handleAdd() {
  handleTableAction({ cityName: queryParams.value.cityName }, 'add')
}
function handleTableAction(obj, operType) {
  if (operType == 'auth') {
    disableTag(obj.tagId).then(() => {
      proxy.$modal.msgSuccess('操作成功')
      getList()
    })
  } else {
    let data = { ...obj, actionType: operType }

    dialogRef.value?.init(data)
  }
}

/**init +  onActivated*/
onMountedOrActivated(getList)
</script>
